<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index extends HYD_Controller {
    function __construct(){
        parent::__construct();
        //$this->load->library('admin');
        if (!$this->admin->is_logged_on()) {            
            redirect('login');
        }
    }
    public function index(){
        $module = $this->input->get_post('module');
        $controller = $this->input->get_post('controller');
        $action = $this->input->get_post('action');
        
        if ((!empty($module) && !empty($action)) || (!empty($action))) {
            try {
                $response = $this->ajax($module, $action, $controller);
                if (!empty($response)) {
                    $this->admin->do_userlog(__CLASS__, __FUNCTION__, "{$module}:{$controller}:{$action}", '', TRUE, FALSE);
                }
            } catch (Exception $e) {
                $php_error_msg = "\nMessage: " . $e->getMessage() . "\nFile: " . $e->getFile() . "\nLine: " . $e->getLine() . "\nCode: " . $e->getCode() . "\n";
                $error_message = sprintf($this->lang->line('ms_error_internal_exception'), $php_error_msg);
                //$response = array('success' => FALSE, 'feedback' => $error_message);
                $this->admin->do_userlog(__CLASS__, __FUNCTION__, "{$module}:{$controller}:{$action}", $response['feedback'], $response['success'], TRUE);
            }
            if (!empty($response)) {
                $this->set_output($response);
            }
        }else{
            //log_message('Debug', '-------------'.$module.$action.'--------------');
            $userdata = $this->session->userdata('user_data');
            $this->load->view('admin',$userdata);
        }
    }

    /**
     * Handle ajax request generated by extjs component
     *
     * Run the action in one module and the response must be an array
     *
     * @access  private
     * @return  array
     */
    public function ajax($module, $action, $controller = NULL) {
        $response = '';
        if (!empty($module) && !empty($action)) {
//指定控制器名称
            if (!empty($controller)){
                require_once $module . '/' . $controller . '.php';
            }else{
                require_once $module . '/' . $module. '.php';
            }
            $this->$module = new $module();

//$this->load->module($module);
            if (is_callable(array($this->$module, $action))) {
                $response = $this->$module->$action();
            }
        } else {
            $action = $this->input->get_post('action');

            if (method_exists($this, $action)) {
                $response = $this->$action();
            }
        }
        return $response;
    }
}

/* End of file index.php */
/* Location: ./application/controllers/index.php */